#e
#Title[gʏ3]
#Text[]
#ScriptVersion[2]

script_enemy_main
{
	let cx=GetCenterX();
	let cy=GetCenterY();
	let arrayLaser=[];
	let imgExRumia=GetCurrentScriptDirectory~"img\ExRumia.png";
	let imgExRumiaBack=GetCurrentScriptDirectory~"img\ExRumiaBack.png";
	LoadUserShotData(GetCurrentScriptDirectory~"shot.txt");
	@Initialize
	{
		SetMovePosition02(cx,cy,60);
		SetLife(3000);
		SetDamageRate(100,100);
		SetTimer(60);
		SetGraphicRect(1,1,64,64);
		SetInvincibility(60);
		LoadGraphic(imgExRumia);
		SetTexture(imgExRumia);	
		shottask;
		movetask;
		SetShotAutoDeleteClip(256,256,256,256);
	}
	@MainLoop
	{
			yield;
			SetCollisionA(GetX(),GetY(),24);
			SetCollisionB(GetX(),GetY(),24);
	}
	@Finalize
	{
	}
	@DrawLoop
	{
		SetGraphicRect(1,1,64,64);
		SetAlpha(255);
		DrawGraphic(GetX(),GetY());
	}
	task shottask
	{
		let s=0;
		wait(60);
		CreateObjLaser(GetX,GetY,0,128,5,RED01,0);
		loop
		{
			wait(3);
			shot(rand(GetClipMinX,GetClipMaxX),GetClipMaxY+32);
			shot(rand(GetClipMinX,GetClipMaxX),GetClipMinY-32);
			shot(GetClipMaxX+32,rand(GetClipMinY,GetClipMaxY));
			shot(GetClipMinX-32,rand(GetClipMinY,GetClipMaxY));
		}
	}
	task movetask
	{
		yield;
		loop
		{
			wait(90);
			move(rand(120, 160),rand(-40, 40),60,GetClipMinX+32,GetClipMinY+64,GetClipMaxX-32,cy);
			wait(90);
		}
	}
	task CreateObjLaser(let x, let y, let angle, let length,let width, let graphic, let delay)
	{
		let obj = Obj_Create(OBJ_LASER);
		arrayLaser = arrayLaser~[obj];//
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, GetAngleToPlayer+angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		ObjLaser_SetLength(obj, length);
		ObjLaser_SetWidth(obj, width);
		ObjLaser_SetSource(obj, false);
	        loop(delay){ yield; }
		CreateSubLaser(ObjLaser_GetEndX(obj),ObjLaser_GetEndY(obj),150+angle,64,5,RED01,0);
		CreateSubLaser(ObjLaser_GetEndX(obj),ObjLaser_GetEndY(obj),-150+angle,64,5,RED01,0);
		while(!Obj_BeDeleted(obj))
		{
			Obj_SetPosition(obj, GetX, GetY);
			Obj_SetAngle(obj, GetAngleToPlayer+angle);
			yield;
		}
	}
	task CreateSubLaser(let x, let y, let angle, let length,let width, let graphic, let delay )
	{
		let obj = Obj_Create(OBJ_LASER);
		let s=0;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		ObjLaser_SetLength(obj, length);
		ObjLaser_SetWidth(obj, width);
		ObjLaser_SetSource(obj, false);
	        loop(delay){ yield; }
		while(!Obj_BeDeleted(obj))
		{
			Obj_SetPosition(obj, ObjLaser_GetEndX(arrayLaser[0]), ObjLaser_GetEndY(arrayLaser[0]));
			Obj_SetAngle(obj,GetAngleToPlayer+angle);
			yield;
		}
	}
	task CreateObjShot(let x, let y, let speed,let angle, let graphic, let delay)
	{
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, speed);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
	        loop(delay){ yield; }
		while(!Obj_BeDeleted(obj))
		{
			Obj_SetAngle(obj, GetAngleToPlayer);
			yield;
		}
	}
	function shot(let shotx,let shoty)
	{
		CreateObjShot(shotx, shoty, 2,GetAngleToPlayer, 105, 0)
	}
	function move(xMove, yAdd, frame, left, top, right, bottom)
	{
    		let x;
    		let y;
		if(GetPlayerX < GetX) 
		{
			x = GetX - xMove;
			if(x < left) 
			{
				x = GetX + xMove;
			}
		} 
		else {
			x = GetX + xMove;
			if(right < x)
			{
				x = GetX - xMove;
			}
		}
		y = GetY + yAdd;
		if(y < top)
		{
			y = top;
		}
		else if(bottom < y)
		{
			y = bottom;
		}
  			SetMovePosition02(x, y, frame);
	}
	function wait(w) 
	{
		loop(w)
		{
			yield;
		}
	}
}